<?php

class Ext_Attachment_Shopping_Add
{
    protected $_articleId;

    protected $_messager;
    const VIDEO_EXISTS              = 'shoppingExists';
    protected $_messageTemplates = array(
        self::VIDEO_EXISTS              => "Shopping '%name%' already exists.",
    );
    
    public function __construct($articleId)
    {
        $this->_articleId = $articleId;
        $this->_messager  = new Ext_Messager($this->_messageTemplates);
    }

    public function add($data)
    {
        $result = array(
            'code'     => Ext_Attachment_Shopping_Add_Result::FAILURE,
            'data'     => '',
            'messages' => array(),
        );
        
        $shoppingIdArray = array();
        $articleShopping = App_Model_DbTable_Article_Attachment_Shopping::getInstance()->getRowByArticleId($this->_articleId);
        if($articleShopping){
            $ids = $articleShopping['ids'];

            $idsArray = explode(';', $ids);
            for($i=0;$i<count($idsArray);$i++)
            {
                $attachmentShoppingId = $idsArray[$i];
                if(!$attachmentShoppingId){
                    continue;
                }

                $attachmentShopping = App_Model_DbTable_Attachment_Shopping::getInstance()->getRowById($attachmentShoppingId);
                if(!$attachmentShopping){
                    continue;
                }
                $shoppingIdArray[] = $attachmentShopping['shoppingId'];
            }
        }
        
        $shoppingId = $data->shoppingId;
        if(in_array($shoppingId, $shoppingIdArray)){
             $result['messages'][] = $this->_messager->addMessage(self::VIDEO_EXISTS, array('name'=>$data->name));
             return new Ext_Attachment_Shopping_Add_Result($result['code'], $result['data'], $result['messages']);
        }
        
        $row = array(
            'articleId'     => $this->_articleId,
            'shoppingId'    => $shoppingId,
            'name'          => $data->name,
        );
        $attachmentShoppingId = App_Model_DbTable_Attachment_Shopping::getInstance()->addNew($row);
        
        $shoppingr = new Ext_Article_Attachment_Shopping($this->_articleId);
        $shoppingr->add($attachmentShoppingId);
        
        $article = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$article){
            throw new Ext_Attachment_Shopping_Add_Exception("Article record not found.");
        }
        
        $attachmentsArray = unserialize($article['attachments']);
        if(!is_array($attachmentsArray)){
            $attachmentsArray = array();
        }
        $attachment = array("type"=>'shopping', "id"=>$attachmentShoppingId);
        array_unshift($attachmentsArray, $attachment);
        if($attachmentsArray){
            $row = array(
                'attachments'  => serialize($attachmentsArray),
            );
            App_Model_DbTable_Article::getInstance()->addOld($row, $this->_articleId);
        }
        
        $result['code'] = Ext_Attachment_Shopping_Add_Result::SUCCESS;
        $result['data'] = $attachmentShoppingId;
        return new Ext_Attachment_Shopping_Add_Result($result['code'], $result['data'], $result['messages']);
    }
}
